package com.appkubes.fintech.admin.application.bill.service.impl;

import cn.hutool.core.date.DateTime;
import com.appkubes.fintech.admin.application.bill.entity.dto.RepayVO;
import com.appkubes.fintech.admin.application.bill.enums.RentRePayOrderEnums;
import com.appkubes.fintech.admin.application.bill.service.IRePayOrderService;
import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.core.po.FintechRentBillHeader;
import com.appkubes.fintech.core.po.FintechRentBillLines;
import com.appkubes.fintech.core.po.FintechRentRepayOrder;
import com.appkubes.fintech.core.po.FintechUserBaseInfo;
import com.appkubes.fintech.core.pojo.FintechRentRepayOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

@Service
public class RePayOrderServiceImple implements IRePayOrderService {


    @Resource
    private FintechRentRepayOrderMapper orderMapper;


    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Response<FintechRentRepayOrder> createOrder(RepayVO vo, FintechUserBaseInfo baseInfo, FintechRentBillHeader header,Integer periodType) {
        FintechRentRepayOrder order=new FintechRentRepayOrder();
        order.setUserKey(baseInfo.getUserKey());
        order.setUserName(baseInfo.getRealName());
        order.setPayAmt(vo.getPayAmt().multiply(new BigDecimal(100)).intValue());
        order.setRepayKey(UUID.randomUUID().toString().replace("-",""));
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setPayTime(new Date());
        order.setBillKey(header.getBillKey());
        order.setStatus(RentRePayOrderEnums.未支付.getValue());
        order.setRentOrderKey(header.getOrderKey());
        order.setPayType("03");
        order.setRetPayMsg("支付中");
        order.setPeriodType(periodType); // 账单类型
        orderMapper.insert(order);
        return Response.success(order);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Response<FintechRentRepayOrder> createOrder(FintechRentBillLines lines,RepayVO vo, FintechUserBaseInfo baseInfo, FintechRentBillHeader header,Integer periodType) {
        FintechRentRepayOrder order=new FintechRentRepayOrder();
        order.setUserKey(baseInfo.getUserKey());
        order.setBillKey(header.getBillKey());
        order.setBillLineKey(lines.getBillLineKey());
        order.setLinePeriods(lines.getPeriod().toString());
        order.setUserName(baseInfo.getRealName());
        order.setPayAmt(vo.getPayAmt().multiply(new BigDecimal(100)).intValue());
        order.setRepayKey(UUID.randomUUID().toString().replace("-",""));
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setRentOrderKey(header.getOrderKey());
        order.setPayType("03");
        order.setPayTime(new Date());
        order.setPeriodType(periodType);
        order.setRetPayMsg("支付中");
        order.setStatus(RentRePayOrderEnums.支付中.getValue());
        orderMapper.insert(order);
        return Response.success(order);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Response paySuc(String repayOrderKey) {
        FintechRentRepayOrder order=    orderMapper.selectByPrimaryKey(repayOrderKey);
        if(order==null){
            return Response.fail("对象不存在");
        }
        order.setStatus(RentRePayOrderEnums.支付成功.getValue());
        order.setPayTime(new Date());
        order.setRetPayMsg("支付成功");
        order.setPayTradeNo("");
        order.setUpdateTime(new DateTime());
        orderMapper.updateByPrimaryKey(order);
        return Response.success();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Response payErr(String repayOrderKey,String msg) {
        FintechRentRepayOrder order=    orderMapper.selectByPrimaryKey(repayOrderKey);
        if(order==null){
            return Response.fail("对象不存在");
        }
        order.setStatus(RentRePayOrderEnums.支付失败.getValue());
        order.setPayTime(new Date());
        order.setRetPayMsg(msg);
        order.setPayTradeNo("");
        order.setUpdateTime(new DateTime());
        orderMapper.updateByPrimaryKey(order);
        return Response.success();

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Response repaySuc(String repayOrderKey,String periods) {
        FintechRentRepayOrder order=    orderMapper.selectByPrimaryKey(repayOrderKey);
        if(order==null){
            return Response.fail("对象不存在");
        }
        order.setStatus(RentRePayOrderEnums.还款成功.getValue());
        order.setReapayTime(new Date());
        order.setRetPayMsg("支付成功");
        order.setRetRepayMsg("还款成功");
        order.setLinePeriods(periods);
        order.setPayTradeNo("");
        order.setUpdateTime(new DateTime());
        orderMapper.updateByPrimaryKey(order);
        return Response.success();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Response repayErr(String repayOrderKey,String periods,String msg) {
        FintechRentRepayOrder order=    orderMapper.selectByPrimaryKey(repayOrderKey);
        if(order==null){
            return Response.fail("对象不存在");
        }
        order.setRetPayMsg("支付失败");
        order.setStatus(RentRePayOrderEnums.还款失败.getValue());
        order.setReapayTime(new Date());
        order.setRetRepayMsg(msg);
        order.setLinePeriods(periods);
        order.setPayTradeNo("");
        order.setUpdateTime(new DateTime());
        orderMapper.updateByPrimaryKey(order);
        return Response.success();
    }
}
